package com.onccc.social.step

import android.app.ActivityManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.util.Log
import android.view.accessibility.AccessibilityNodeInfo
import com.blankj.utilcode.util.Utils
import com.onccc.social.Assists
import com.onccc.social.Assists.click
import com.onccc.social.Assists.findFirstParentClickable
import com.onccc.social.Assists.getBoundsInScreen
import com.onccc.social.Assists.getChildren
import com.onccc.social.Assists.getNodes
import com.onccc.social.Assists.log
import com.onccc.social.Assists.paste
import com.onccc.social.App
import com.onccc.social.OverManager
import com.onccc.social.stepper.Step
import com.onccc.social.stepper.StepCollector
import com.onccc.social.stepper.StepImpl
import kotlinx.coroutines.delay


class PublishWxVideo : StepImpl() {

    var selectIndex = 0
    var maxSelect = 1

    var title: String = "快来围观我的微信小视频"


    override fun onImpl(collector: StepCollector) {
        collector.next(StepTag.STEP_1) { it ->
            OverManager.log("启动微信")
            Intent().apply {
                addCategory(Intent.CATEGORY_LAUNCHER)
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                component = ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI")
                Assists.service?.startActivity(this)
            }
            it.data?.let {
//                maxSelect = it as Int
            }
            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_2) {

            Assists.findByText("我").forEach {
                val screen = it.getBoundsInScreen()
                if (screen.left > 630 && screen.top > 1850) {
                    OverManager.log("已打开微信主页，点击【我】")
                    it.parent.parent.click()
                    runIO { delay(1522) }
                    return@next Step.get(StepTag.STEP_3)
                }
            }

            if (Assists.getPackageName() == App.TARGET_PACKAGE_NAME) {
                Assists.back()
                return@next Step.get(StepTag.STEP_2)
            }

            if (it.repeatCount == 5) {
                return@next Step.get(StepTag.STEP_1)
            }

            return@next Step.repeat
        }.next(StepTag.STEP_3) {
            Assists.findByText("视频号").forEach {
                val screen = it.getBoundsInScreen()
                if (screen.top > 1000 && screen.bottom > 1000) {
                    if (it.isClickable) {
                        it.click()
                        runIO { delay(1372) }
                    } else {
                        it.findFirstParentClickable()?.let { pat ->
                            pat.log()
                            pat.click()
                            runIO { delay(1372) }
                        }
                    }
                    return@next Step.get(StepTag.STEP_4)
                }
            }
            return@next Step.none

        }.next(StepTag.STEP_4) {

            Assists.findByText("发表视频").firstOrNull() {
                OverManager.log("已进入发表视频")
                val screen = it.getBoundsInScreen()
                Log.e("==========:", "left : " + screen.left + " top : " + screen.top)
                it.findFirstParentClickable()?.let {
                    it.log()
                    it.click()
                }
                return@next Step.get(StepTag.STEP_5)
            }
            if (it.repeatCount == 5) {
                OverManager.log("未进入发表视频")
                val s = Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
                s.killBackgroundProcesses("com.tencent.mm")
                runIO {
                    delay(2000)
                }
                return@next Step.get(StepTag.STEP_1)
            }
            return@next Step.repeat
        }.next(StepTag.STEP_5) {
            runIO { delay(2000) }
            Assists.findByText("从相册选择").firstOrNull()?.let {
                OverManager.log("已进入发表视频")
                val screen = it.getBoundsInScreen()
                Log.e("==========:", "left : " + screen.left + " top : " + screen.top)
                it.findFirstParentClickable()?.let {
                    it.log()
                    it.click()
                    return@next Step.get(StepTag.STEP_6)
                }
            }
            return@next Step.none
        }.next(StepTag.STEP_6) {
            Assists.findByTags("androidx.recyclerview.widget.RecyclerView").forEach {
                it.getChildren().forEach {
                    it.log("getChildren")
                }
                if (selectIndex >= 0 && selectIndex < it.childCount) {
                    it.getChild(1 + selectIndex).let { cd ->
                        cd.getNodes().forEach { sub ->
                            if (TextUtils.equals("android.widget.CheckBox", sub.className)) {
                                sub.log("SUB CheckBox")
                                sub.click()
                                runIO { delay(1500) }
                                return@next Step.get(StepTag.STEP_7)
                            }
                        }
                    }
                } else {
                    return@next Step.get(StepTag.STEP_10)
                }
            }
            return@next Step.none
        }.next(StepTag.STEP_7) {
            OverManager.log("点击下一步")
            Assists.findByText("下一步").forEach {
                it.click()
                runIO { delay(2100) }
                return@next Step.get(StepTag.STEP_8)
            }
            return@next Step.none
        }.next(StepTag.STEP_8) {
            OverManager.log("点击完成")
            Assists.findByText("完成").forEach {
                it.log("完成")
                it.click()
                runIO { delay(1589) }
                return@next Step.get(StepTag.STEP_9)
            }
            return@next Step.none

        }.next(StepTag.STEP_9) {

            OverManager.log("输入内容")
            Assists.findByTags("android.widget.EditText").forEach {
                it.performAction(AccessibilityNodeInfo.ACTION_FOCUS)
                it.paste(title)
                runIO { delay(1300) }
            }
            OverManager.log("点击发表")
            Assists.findByText("发表").forEach {
                it.click()
                runIO { delay(15000) }
                return@next Step.get(StepTag.STEP_10)
            }
            return@next Step.none
        }.next(StepTag.STEP_10) {
//            selectIndex++
            if (selectIndex >= maxSelect) {
                return@next Step.none
            }
            val s = Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            s.killBackgroundProcesses("com.tencent.mm")
            runIO {
                delay(2000)
            }
            return@next Step.none
        }
    }

}